package com.hopu.service.impl;

import com.hopu.domain.ContractProductCVo;
import com.hopu.domain.ContractVO;
import com.hopu.domain.ExtCproductCVo;
import com.hopu.domain.export.ExportC;
import com.hopu.domain.export.ExportProductC;
import com.hopu.domain.export.ExtEproductC;
import com.hopu.mapper.ContractCMapper;
import com.hopu.mapper.ContractVOMapper;
import com.hopu.mapper.export.ExportCMapper;
import com.hopu.mapper.export.ExportProductCMapper;
import com.hopu.mapper.export.ExtEproductCMapper;
import com.hopu.service.ExportCService;
import com.hopu.util.UtilFuns;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class ExportCServiceImpl implements ExportCService {
    @Autowired
    public ExportCMapper exportCMapper;

    @Autowired
    ContractVOMapper contractVOMapper;
    @Autowired
    ContractCMapper contractCMapper;
    @Autowired
    ExportProductCMapper exportProductCMapper;
    @Autowired
    ExtEproductCMapper extEproductCMapper;

    @Override
    public List<ExportC> selectByExample() {
        return exportCMapper.find();
    }

    @Override
    public void insert(String[] contractIds) {

        /*
         * 步骤：
         * 1、根据合同id获得合同对象，获取合同号
         * 2、将合同下的货物信息搬家到报运下的货物表中
         * 3、将合同下的附件信息搬家到报运下的附件表中
         */

        //拼接合同号，报运号
        String contractNos = "";
        for(int i=0;i<contractIds.length;i++) {
            ContractVO view = contractVOMapper.view(contractIds[i]);

            //以空格作为分隔符
            contractNos += view.getContractNo() + " ";
        }
        //工具类，删除最后一个字符
        contractNos = UtilFuns.delLastChar(contractNos);

        ExportC export = new ExportC();
        export.setExportId(UUID.randomUUID().toString());

        //工具类，拼串
        export.setContractIds(UtilFuns.joinStr(contractIds, ","));
        export.setCustomerContract(contractNos);
        //0草稿
        export.setState(0);

        exportCMapper.insert(export);

        for(int i=0;i<contractIds.length;i++){
            ContractVO view = contractVOMapper.view(contractIds[i]);

            for(ContractProductCVo cp:view.getContractProductcS()){
                ExportProductC ep = new ExportProductC();
                ep.setExportProductId(UUID.randomUUID().toString());
                ep.setExportId(export.getExportId());
                //数据搬家，将合同下的对应的货物信息写入到报运下的货物信息中
                ep.setFactoryId(cp.getFactoryId());
                ep.setFactoryName(cp.getFactorycs().getFactoryName());
                ep.setProductNo(cp.getProductNo());
                ep.setPackingUnit(cp.getPackingUnit());
                BigDecimal a = new BigDecimal(cp.getCnumber().toString());
                ep.setCnumber(a);
                BigDecimal b = new BigDecimal(cp.getBoxNum().toString());
                ep.setBoxNum(b);
                BigDecimal c = new BigDecimal(cp.getPrice().toString());
                ep.setPrice(c);
                exportProductCMapper.insert(ep);
                for(ExtCproductCVo extCproductCVo:cp.getExtCproductCVos()){
                    ExtEproductC extep = new ExtEproductC();
                    BeanUtils.copyProperties(extCproductCVo, extep);
                    extep.setExtEproductId(UUID.randomUUID().toString());
                    extep.setExportProductId(ep.getExportProductId());
                    extep.setFactoryId(extCproductCVo.getFactoryId());
                    extep.setFactoryName(extCproductCVo.getFactorycs().getFactoryName());
                    extEproductCMapper.insert(extep);
                }
            }

        }
    }

    @Override
    public void state(Map<String, Object> map) {
        exportCMapper.state(map);
    }

    @Override
    public ExportC selectByPrimaryKey(String exportId) {
        return exportCMapper.selectByPrimaryKey(exportId);
    }

    @Override
    public String getMrecordData(String[] exportId) {
            StringBuffer sBuf = new StringBuffer();
        for (String s : exportId) {
            List<ExportProductC> exportProductCS = exportProductCMapper.selectByList(s);
            for (ExportProductC ep : exportProductCS) {
            sBuf.append("addTRRecord(\"mRecordTable\", \"").append(ep.getExportProductId()).append("\", \"")
                    .append(ep.getProductNo()).append("\", \"").append(ep.getCnumber()).append("\", \"")
                    .append(UtilFuns.convertNull(ep.getGrossWeight())).append("\", \"")
                    .append(UtilFuns.convertNull(ep.getNetWeight())).append("\", \"")
                    .append(UtilFuns.convertNull(ep.getSizeLength())).append("\", \"")
                    .append(UtilFuns.convertNull(ep.getSizeWidth())).append("\", \"")
                    .append(UtilFuns.convertNull(ep.getSizeHeight())).append("\", \"")
                    .append(UtilFuns.convertNull(ep.getExPrice())).append("\", \"")
                    .append(UtilFuns.convertNull(ep.getTax())).append("\");");
            }
        }

        return sBuf.toString();
    }

    @Override
    public void update(ExportC exportC,
                       String[]  del,
                       Integer[] mr_orderNo,
                       Integer[] mr_cnumber,
                       Double[] mr_grossWeight,
                       Double[] mr_netWeight,
                       Double[] mr_sizeLength,
                       Double[] mr_sizeWidth,
                       Double[] mr_sizeHeight,
                       Double[] mr_exPrice, Double[] mr_tax) {
        if (exportC!=null){
        Integer num =0;
        exportC.setState(num);
         exportCMapper.updateByPrimaryKey(exportC);
        }
        //批量修改货物信息
        for(int i=0;i<del.length;i++){			//修改标识，只有用户修改的行才进行更新
            ExportProductC ep = exportProductCMapper.selectByPrimaryKey(del[i]);
                ep.setOrderNo(new BigDecimal(mr_orderNo[i].toString()));
                ep.setCnumber(new BigDecimal(mr_cnumber[i].toString()));
                ep.setGrossWeight(new BigDecimal(Double.toString(mr_grossWeight[i])));
                ep.setNetWeight(new BigDecimal(Double.toString(mr_netWeight[i]) ));
                ep.setSizeLength(new BigDecimal(Double.toString(mr_sizeLength[i])));
                ep.setSizeWidth(new BigDecimal(Double.toString(mr_sizeWidth[i])));
                ep.setSizeHeight(new BigDecimal(Double.toString(mr_sizeHeight[i])));
                ep.setExPrice(new BigDecimal(Double.toString(mr_exPrice[i])));
                ep.setTax(new BigDecimal(Double.toString(mr_tax[i])));
                    exportProductCMapper.updateByPrimaryKey(ep);
        }
    }

    @Override
    public int deleteByPrimaryKey(String exportId) {
        return exportCMapper.deleteByPrimaryKey(exportId);
    }
}
